using System;
using System.Collections.Generic;
using System.Text;
using Entities;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
namespace DAL
{
   public class tbTaikhoanDAL
    {
        private SqlConnection cn = null;
   public tbTaikhoanDAL()    
   {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(tbTaikhoan tk)
        {
            SqlCommand cm = new SqlCommand("taikhoan_insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@tendangnhap", tk.Tendangnhap);
            cm.Parameters.AddWithValue("@matkhau", tk.Matkhau);
            cm.Parameters.AddWithValue("@chucvu", tk.Chucvu);
            cm.ExecuteNonQuery();
        }
        public void Update(tbTaikhoan tk)
        {
            SqlCommand cm = new SqlCommand("taikhoan_update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@mataik", tk.Mataik );
            cm.Parameters.AddWithValue("@tendangnhap",tk.Tendangnhap);
            cm.Parameters.AddWithValue("@matkhau", tk.Matkhau);
            cm.Parameters.AddWithValue("@chucvu", tk.Chucvu);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("taikhoan_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@mataik", iD);
            cm.ExecuteNonQuery();
        }
      public tbTaikhoanCollection Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
      {
          SqlCommand cm = new SqlCommand("taikhoan_get", cn);
          cm.CommandType = CommandType.StoredProcedure;
          cm.Parameters.AddWithValue("@Where", where);
          cm.Parameters.AddWithValue("@PageIndex", pageIndex);
          cm.Parameters.AddWithValue("@PageSize", pageSize);
          cm.Parameters.AddWithValue("@OrderBy", orderBy);
          cm.Parameters.AddWithValue("@OrderDirection",orderDirection);
          cm.Parameters.Add(new SqlParameter("@TotalRecords",DbType.Int32)).Direction =ParameterDirection.Output;
          SqlDataReader dr = cm.ExecuteReader();
          tbTaikhoanCollection tkCollection= new tbTaikhoanCollection();
          while (dr.Read())
          {
             tbTaikhoan tk=new tbTaikhoan();
             tk.Mataik = int .Parse(dr.GetValue(0).ToString());
             tk.Tendangnhap= (dr.GetValue(1).ToString());
             tk.Matkhau = (dr.GetValue(2).ToString());
             tk.Chucvu = (dr.GetValue(3).ToString());
             tkCollection.Add(tk);
          }
          dr.Close();
          totalRecords = int.Parse(
          cm.Parameters["@TotalRecords"].Value.ToString());
          return tkCollection;
      }
    }
}
